import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lily
 * Date: 2024-10-01
 * Time: 0:52
 */
public class Subsets {
    static List<List<Integer>> ret;
    static List<Integer> path;
    static int flag;

    public static List<List<Integer>> subsets(int[] nums) {
        ret = new ArrayList<>();
        path = new ArrayList<>();
        dfs(nums, 0);
        return ret;
    }

    public void dfs1(int[] nums, int pos)
    {
        if(pos == nums.length)
        {
            ret.add(new ArrayList<>(path));
            return;
        }
        // 选
        path.add(nums[pos]);
        dfs1(nums, pos + 1);
        path.remove(path.size() - 1); // 恢复现场
        // 不选
        dfs1(nums, pos + 1);
    }

    private static void dfs(int[] nums, int pos) {
            ret.add(new ArrayList<>(path));

        for (int i = pos; i < nums.length; i++) {

            path.add(nums[i]);
            dfs(nums, i + 1);
            //回溯
            path.remove(path.size() - 1);

        }
        return;
    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 3};
        System.out.println(subsets(nums));
    }
}
